#include<iostream>
using namespace std;

class Solution
{
public:
    int myAtoi(string str)
    {
        int res = 0; int criticalvalue = INT_MAX / 10;
        int i = 0, flag = 1, len = str.size();
        while (str[i] == ' ')
        {
            if (++i == len)
            {
                return 0;
            }
        }
        if (str[i] == '-')
        {
            flag = -1;
        }
        if (str[i] == '-' || str[i] == '+')
        {
            i++;
        }
        for (int j = i; i < len; j++)
        {
            if (str[j] - '0' < 0 || str[j] - '0' > 9)
            {
                break;
            }
            if (res > criticalvalue || res == criticalvalue && str[j] - '0' > 7)
            {
                return flag == 1 ? INT_MAX : INT_MIN;
            }
            res = res * 10 + (str[j] - '0');
        }

        return res * flag;
    }
};

int main()
{
    char str[] = "  -35";
    Solution s;
    int ret=s.myAtoi(str);
    cout << ret << endl;
    return 0;

}